package cn.icatw.gulimall.ware.service.impl;

import cn.icatw.common.utils.PageUtils;
import cn.icatw.common.utils.Query;
import cn.icatw.common.utils.R;
import cn.icatw.gulimall.ware.dao.WareInfoDao;
import cn.icatw.gulimall.ware.entity.WareInfoEntity;
import cn.icatw.gulimall.ware.feign.MemberFeignService;
import cn.icatw.gulimall.ware.service.WareInfoService;
import cn.icatw.gulimall.ware.vo.FareVo;
import cn.icatw.gulimall.ware.vo.MemberAddressVo;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;


/**
 * impl制品信息服务
 *
 * @author icatw
 * @date 2022/03/26
 */
@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                new QueryWrapper<WareInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("id", key).or().like("name", key)
                    .or().like("address", key)
                    .or().like("areacode", key);
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        R r = memberFeignService.addrInfo(addrId);
        MemberAddressVo memberAddressVo = r.getData("memberReceiveAddress", new TypeReference<MemberAddressVo>() {
        });
        if (memberAddressVo != null) {
            //运费
            String phone = memberAddressVo.getPhone();
            String fare = phone.substring(phone.length() - 1, phone.length());
            //String substring = "9";
            fareVo.setFare(new BigDecimal(fare));
            //收货人名字+收货人地址
            fareVo.setAddress(memberAddressVo);
            return fareVo;
        }
        return null;
    }
}

